﻿

$(function () {
    loadMenu();
    
    var $eq_appbar_typo_title = $('#eq-appbar-typo-title');
    $eq_appbar_typo_title.html('控制面板');
    document.getElementById('eq-dashboard').addEventListener('show.mdui.tab', function () {
        $eq_appbar_typo_title.html('控制面板');
    });
});

function loadMenu() {

    $.ajax({
        url: '/Manage/GetAuthorizedMenus',
        async: true,
        dataType:'json',
        success: function (data) {
            $('#eq-main-drawer-list').empty().append(initMenuStr(data));
        }
    });
 
}

function initMenuStr(menus) {
    var str = '';
    for (var i = 0; i < menus.length; i++) {
        var itemopen = menus[i].itemopen;
        
        str += '<div class="mdui-collapse-item ';
        if (itemopen === true) {
            str += 'mdui-collapse-item-open';
        }
        str += '">';
        str += '<div class="mdui-collapse-item-header mdui-list-item mdui-ripple">';
        str += '<i class="mdui-list-item-icon mdui-icon material-icons">' + menus[i].icon + '</i>';
        str += '<div class="mdui-list-item-content">' + menus[i].text + '</div>';
        str += '<i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>';
        str += '</div>';
        str += '<div class="mdui-collapse-item-body mdui-list">';
        for (var j = 0; j < menus[i].children.length; j++) {
            str += '<a data-id="' + menus[i].children[j].id + '" data-text="' + menus[i].children[j].text + '" data-closemark="' + menus[i].children[j].closemark + '" data-url="' + menus[i].children[j].url + '" href="javascript:void(0);" onclick="addTab(this);return false;" class="mdui-list-item mdui-ripple mdui-typo-body-2"><i class="mdui-icon material-icons">' + menus[i].children[j].icon + '</i><span class="mdui-m-l-2">' + menus[i].children[j].text + '</span></a>';
            str += '<div class="mdui-divider mdui-m-y-0"></div>';
            
            
        }
        str += '</div>';
        str += '</div>';
        str += '<div class="mdui-divider mdui-m-y-0 mdui-text-color-white"></div>';
    }
    return str;
}

function addTab(a) {
    var $manageTab = $('#eq-manage-tab');
    var $this = $(a);
    var id = $this.attr('data-id');
    var text = $this.attr('data-text');
    var closemark = $this.attr('data-closemark');
    var url = $this.attr('data-url');
    var inst = new mdui.Tab('#eq-manage-tab');
    var $tabBox = $('#eq-manage-tabcontent');
    var $eq_appbar_typo_title = $('#eq-appbar-typo-title');
    $.cookie('EqwwwCurrentModuleId', id);
    if ($manageTab.find('[id="' + id + '"]').length > 0) {
        inst.show(id);
        return false;
    }

    //$.loading(false, true);
    top.$.loadingLayer(true);

    var newTab = '<a id="' + id + '" href="#' + id + '-box" class="mdui-ripple">';
    newTab += '<div>';
    var shortText = text;
    if (text.length > 4) {
        shortText = text.substr(0, 4) + "...";
    }
    newTab += shortText;
    if (closemark === "true") {
        newTab += '<i class="mdui-icon material-icons mdui-color-pink mdui-typo-body-1 eq-tab-close-btn">close</i>';
    }
    newTab += '</div>';
    newTab += '</a>';
    $manageTab.append(newTab);

    var newTabBox = '<div id="' + id + '-box" style="display:none;">';
    newTabBox += '<iframe id="'+id+'-iframe" name="'+id+'-iframe" frameborder="0" src="' + url +'"  style="width:100%;"></iframe>';
    newTabBox += '</div>';
    $tabBox.append(newTabBox);

    inst.handleUpdate();

    document.getElementById(id).addEventListener('show.mdui.tab', function () {
        $.cookie('EqwwwCurrentModuleId',id);
        $eq_appbar_typo_title.html(text);
    });


    inst.show(id);

    $('#' + id).find('i').bind('click', function (e) {
        removeTab(this);
        e.stopPropagation();
        e.preventDefault();
    });
    handlerIframeHeight();
}

function removeTab(a) {
    var $manageTab = $('#eq-manage-tab');
    var $this = $(a).parent().parent();
    var id = $this.attr('id');
    var targetId = id + "-box";
    
    var inst = new mdui.Tab('#eq-manage-tab');
    var $tabBox = $('#eq-manage-tabcontent');
    inst.prev();
    $this.remove();
    $('#' + targetId).remove();
    inst.handleUpdate();
    
}